package models;

import java.sql.Date;
import java.util.List;

import javax.persistence.*;

import play.data.validation.*;
import play.db.jpa.Model;

@Entity
public class Comment extends Model {

	@OneToMany (mappedBy = "relatedComment", cascade=CascadeType.ALL)
	public List<Role> roles;
	@Required
	@ManyToOne
	private User sender;
	@Required
	// @ManyToOne
	private String type;
	@Required
	private String content;
	@Required
	private boolean spam;
	@Required
	private Date timestamp;
	@ManyToOne
	private Plan plan;
	@ManyToOne
	private Idea idea;
	@ManyToOne
	private Request request;
	@ManyToOne
	private Topic topic;

	// comment constructor Author :Nora
	public Comment() {
		this.sender = new User();
		this.content = "";
		this.spam = false;
		this.timestamp = new Date(new java.util.Date().getTime());
	}

	/**
	 * Constructor for a Comment
	 * 
	 * @author Nader Alexan
	 * @modifier Arwa Ismail
	 * @version
	 * @versionModified 2011.0502
	 * @category C4S8
	 * @params sender: The user whose submitting a comment, content: the comment
	 *         itself, type: the type of model whether plan/request/idea that
	 *         the comment is going to be added to, spam: indicator whether it's
	 *         a spam or not, timestamp: the time the comment was posted,
	 *         modelID: the ID of the model of type specified in 'type'
	 */
	public Comment(User sender, String content, String type, boolean spam, long modelID) {
		this.sender = sender;
		this.content = content;
		this.spam = spam;
		this.timestamp = new Date(new java.util.Date().getTime());

		if (type.equalsIgnoreCase("Idea")) {
			Idea idea = Idea.findById(modelID);

			this.idea = idea;
		} else

		{
			if (type.equalsIgnoreCase("Plan")) {
				Plan plan = Plan.findById(modelID);
				this.plan = plan;
			} else {
				if (type.equalsIgnoreCase("Request")) {
					Request request = Request.findById(modelID);

					this.request = request;
				} else {
					if (type.equalsIgnoreCase("Topic")) {
						Topic topic = Topic.findById(modelID);

						this.setTopic(topic);
					}
				}
			}
		}

		this.save();
	}

	// adding request

	public User getSenderId() {
		return sender;
	}

	public void setSenderId(User sender) {
		this.sender = sender;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public User getSender() {
		return sender;
	}

	public void setSender(User sender) {
		this.sender = sender;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

	public Request getRequest() {
		return request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public Idea getIdea() {
		return idea;
	}

	public void setIdea(Idea idea) {

			this.idea = idea;
			idea.addComment(this);
	
	}

	public boolean isSpam() {
		return spam;
	}

	public void setSpam(boolean spam) {
		this.spam = spam;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}

	public void setTopic(Topic topic) {
		
			this.topic = topic;
			topic.addComment(this);
		
	}

	public Topic getTopic() {
		return topic;
	}
}